<template>
  <div class="info">
    <!-- 新闻列表页 -->
    <template>
      <el-skeleton :loading="loading" />
    </template>
    <template v-if="!loading">
      <el-form :model="searchInfo" inline>
        <el-form-item>
          <el-input clearable v-model="searchInfo.title" placeholder="请输入新闻标题"></el-input>
        </el-form-item>
        <el-form-item>
          <el-date-picker v-model="searchInfo.releaseDateArr" value-format="yyyy-MM-dd" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-select v-model="searchInfo.status" placeholder="请选择新闻状态">
            <el-option value="0" label="禁用"></el-option>
            <el-option value="1" label="可用"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="getList">查询</el-button>
          <el-button icon="el-icon-refresh-right" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
      <el-form inline>
        <el-form-item>
          <el-button type="success" @click="openWin()">添加</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="newsList" border>
        <el-table-column label="编号" prop="newsId"/>
        <el-table-column label="新闻标题" prop="title"/>
<!--        <el-table-column label="新闻封面" prop="cover"/>-->
        <el-table-column label="新闻简介" prop="introduction"/>
        <el-table-column label="新闻内容" prop="content" show-overflow-tooltip/>
        <el-table-column label="新闻作者" prop="author"/>
        <el-table-column label="发布日期" prop="releaseDate"/>
        <el-table-column label="新闻详情" width="110px">
          <template slot-scope="scope">
            <el-button type="primary" size="small" @click="showInfo(scope.row.newsId)">新闻详情</el-button>
          </template>
        </el-table-column>
        <el-table-column label="信息状态" prop="status">
          <template slot-scope="scope">
            <el-button type="danger" v-if="scope.row.status==0" @click="changStatus(scope.row)">禁用</el-button>
            <el-button type="success" v-if="scope.row.status==1" @click="changStatus(scope.row)">可用</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150px">
          <template slot-scope="scope">
            <el-button type="success" size="small" @click="openWin(scope.row)">修改</el-button>
            <el-button type="danger" size="small" @click="delInfo(scope.row.newsId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination layout="total,prev,pager,next,sizes,jumper"
                     :total="pageInfo.totalCount"
                     :page-size="pageInfo.size"
                     :current-page="pageInfo.current"
                     :page-sizes="[5,10,20,30,50,100]"
                     @size-change="sizeChange"
                     @current-change="currentChange"
      />
    </template>
    <el-dialog title="新闻详情" :visible.sync="newsInfoVisible">
      <NewsInfo ref="newsInfo"/>
    </el-dialog>
  </div>
</template>

<script>
import NewsInfo from './newsInfo/NewsInfo'
export default {
  name: 'News',
  data () {
    return {
      loading: false,
      searchInfo: {
        title: '',
        releaseDateArr: [],
        status: '',
        page: 1,
        limit: 10
      },
      newsList: [],
      pageInfo: {
        totalCount: 0,
        currPage: 0,
        pageSize: 0,
        totalPage: 0
      },
      newsInfoVisible: false
    }
  },
  components: {
    NewsInfo
  },
  methods: {
    getList () {
      const si = {
        ...this.searchInfo,
        releaseDateArr: this.searchInfo.releaseDateArr.toString()
      }
      this.$http.get('/sys/news/list', si).then(({ data }) => {
        this.loading = false
        if (data.code === 200) {
          this.newsList = data.data.list
          this.pageInfo.totalCount = data.data.totalCount
          this.pageInfo.currPage = data.data.currPage
          this.pageInfo.pageSize = data.data.pageSize
          this.pageInfo.totalPage = data.data.totalPage
        }
      })
    },
    sizeChange (limit) {
      this.searchInfo.limit = limit
      this.getList()
    },
    currentChange (page) {
      this.searchInfo.page = page
      this.getList()
    },
    openWin (newsId) {
      if (newsId) {
        this.$router.push('newsEdit?newsId=' + newsId.newsId)
      } else {
        this.$router.push('newsEdit')
      }
    },
    delInfo (newsId) {
      this.$confirm('确定删除该数据吗？', '询问').then(() => {
        this.$axios.delete(`sys/news/delete/${newsId}`).then(({ data }) => {
          if (data.code === 200) {
            this.$message({
              message: '删除成功',
              type: 'success',
              onClose: () => {
                this.getList()
              }
            })
          } else {
            this.$alert('删除失败！', { type: 'error' })
          }
        })
      })
    },
    resetForm () {
      this.searchInfo.title = ''
      this.searchInfo.releaseDateArr = []
      this.searchInfo.status = ''
      this.getList()
    },
    changStatus (news) {
      this.$confirm(`确定${news.status === 1 ? '禁用' : '启用'}该新闻吗？`).then(() => {
        this.$http.post('/sys/news/update', { newsId: news.newsId, status: news.status === 1 ? 0 : 1 }).then(({ data }) => {
          if (data.code === 200) {
            this.$message.success('更改状态成功')
            this.getList()
          }
        })
      })
    },
    showInfo (newsId) {
      this.newsInfoVisible = true
      this.$nextTick(() => {
        this.$refs.newsInfo.init(newsId)
      })
    }
  },
  created () {
    this.$nextTick(() => {
      this.getList()
    })
  }
}
</script>

<style scoped>

</style>
